package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.utils.wechat.WeiXinUtils;
import cn.com.connext.scrm.web.rest.vm.MerchantRollVM;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: WxbusifavorCallbacksResource
 * @Description:
 * @Author xuebw
 * @Date 2021/9/18
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/wxBusifavorCallbacks/")
public class WxBusifavorCallbacksResource {

    private static Logger log = LoggerFactory.getLogger(WxBusifavorCallbacksResource.class);


    @Autowired
    private WeiXinUtils weiXinUtils;

    /**
     * 设置商家券事件通知地址
     * @param vm
     * @return
     */
    @PostMapping("busifavorCallbacks")
    @ResponseBody
    public JsonResult busifavorCallbacks(@RequestBody MerchantRollVM vm){
        JsonResult jsonResult = new JsonResult();

        if (StringUtils.isBlank(vm.getNotifyUrl())) {
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("参数缺失");
            return jsonResult;
        }
        StringBuffer str = new StringBuffer();
        str.append("https://api.mch.weixin.qq.com/v3/marketing/busifavor/callbacks");
        String url = str.toString();
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.put("notify_url",vm.getNotifyUrl());
        String wxHttpClient = weiXinUtils.postWxHttpClient(url, objectMapper, objectNode);
        log.info("设置商家券事件通知地址res:" + wxHttpClient);
        if (StringUtils.isNotBlank(wxHttpClient)) {
            JSONObject jsonObject = JSONObject.parseObject(wxHttpClient);
            if (null == jsonObject.getInteger("notify_url")) {
                jsonResult.setState(JsonResult.ERROR);
                jsonResult.setMessage("设置商家券事件通知地址失败");
            } else {
                jsonResult.setState(JsonResult.SUCCESS);
                jsonResult.setData(jsonObject);
            }
        } else {
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("设置商家券事件通知地址失败");
        }

        return jsonResult;

    }

    /**
     * 查询商家券事件通知地址
     * @return
     */
    @PostMapping("queryBusifavorCallbacks")
    @ResponseBody
    public JsonResult queryBusifavorCallbacks(){
        JsonResult jsonResult = new JsonResult();

        StringBuffer str = new StringBuffer();
        str.append("https://api.mch.weixin.qq.com/v3/marketing/busifavor/callbacks");
        String url = str.toString();
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode objectNode = objectMapper.createObjectNode();
        String wxHttpClient = weiXinUtils.getWxHttpClient(url, objectMapper, objectNode);
        log.info("查询商家券事件通知地址res:" + wxHttpClient);
        if (StringUtils.isNotBlank(wxHttpClient)) {
            JSONObject jsonObject = JSONObject.parseObject(wxHttpClient);
            if (null == jsonObject.getInteger("mchid")) {
                jsonResult.setState(JsonResult.ERROR);
                jsonResult.setMessage("查询商家券事件通知地址失败");
            } else {
                jsonResult.setState(JsonResult.SUCCESS);
                jsonResult.setData(jsonObject);
            }
        } else {
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("查询商家券事件通知地址失败");
        }

        return jsonResult;

    }
}
